<?php
namespace frontend\controllers;

use Yii;
use yii\base\InvalidParamException;
use yii\web\BadRequestHttpException;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\models\user\LoginForm;
use frontend\models\PasswordResetRequestForm;
use frontend\models\ResetPasswordForm;
use frontend\models\SignupForm;
use frontend\models\ContactForm;
use common\models\user\User;
use common\models\member\Member;
use common\models\category\Category;
use common\models\articles\Articles;
use common\models\meeting\Meeting;
use zhongkefu\zhongkefu;
use zhongkefu\helpers\Utils;
/**
 * Site controller
 */
class SiteController extends Controller
{
    public $layout = 'firstpage';
    /**
     * @inheritdoc
     */
  

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],

        ];
     
    }

    /**
     * Displays homepage.
     *
     * @return mixed
     */
    public function actionIndex()
    {
      
        if(Utils::isMobile()){
            $url = Yii::$app->params['mobileurl'];
            header("Content-type: text/html; charset=utf-8");
            Header("Location: $url");
            exit;
        }


		$conf = false;
		$curpage = !empty(Yii::$app->request->get()["per-page"])?"_".intval(Yii::$app->request->get()["per-page"]):'';
		$filedir = "staticfile/";
		$name = Yii::$app->controller->id."_".Yii::$app->controller->action->id;
		$fail = $curpage.".html";
		$filename = $filedir.$name.$fail;
		if($conf){
			if(!file_exists($filename)){
				$file = $this->render('index',['model'=>'Congratulations!'],true);
				file_put_contents($filename, $file);
			}
			echo file_get_contents($filename);
		}else{
            return $this->render('index',[
            'model'=>'Congratulations!',

            ]);
		}
    }



    /**
     * Displays contact page.
     *
     * @return mixed
     */
    public function actionContact()
    {
        $model = new ContactForm();
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            if ($model->sendEmail(Yii::$app->params['adminEmail'])) {
                Yii::$app->session->setFlash('success', 'Thank you for contacting us. We will respond to you as soon as possible.');
            } else {
                Yii::$app->session->setFlash('error', 'There was an error sending email.');
            }

            return $this->refresh();
        } else {
            return $this->render('contact', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Displays about page.
     *
     * @return mixed
     */
    public function actionAbout()
    {
        return $this->render('about');
    }



    /**
     * Requests password reset.
     *
     * @return mixed
     */
    public function actionRequestPasswordReset()
    {
        $model = new PasswordResetRequestForm();
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            if ($model->sendEmail()) {
                Yii::$app->session->setFlash('success', 'Check your email for further instructions.');

                return $this->goHome();
            } else {
                Yii::$app->session->setFlash('error', 'Sorry, we are unable to reset password for email provided.');
            }
        }

        return $this->render('requestPasswordResetToken', [
            'model' => $model,
        ]);
    }

    /**
     * Resets password.
     *
     * @param string $token
     * @return mixed
     * @throws BadRequestHttpException
     */
    public function actionResetPassword($token)
    {
        try {
            $model = new ResetPasswordForm($token);
        } catch (InvalidParamException $e) {
            throw new BadRequestHttpException($e->getMessage());
        }

        if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword()) {
            Yii::$app->session->setFlash('success', 'New password was saved.');

            return $this->goHome();
        }

        return $this->render('resetPassword', [
            'model' => $model,
        ]);
    }
}
